#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.IO;
using NUnit.Framework;
using Newtonsoft.Utilities.IO;
using Newtonsoft.Utilities.IO.FileSystem;
using Newtonsoft.Utilities.Time;
using System.Linq;

namespace Newtonsoft.Utilities.Tests.IO.FileSystem
{
  public class DirectoryTests : TestsBase
  {
    [Test]
    public void CreateDirectory()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testDirectoryPath = Path.Combine(currentPath, "testdirectory");

      FileUtils.EnsureDirectoryDeleted(testDirectoryPath);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IDirectory directory = fileSystem.GetDirectory(testDirectoryPath);

      Assert.AreEqual(false, directory.Exists);

      directory.Create();

      Assert.AreEqual(true, directory.Exists);
    }

    [Test]
    public void CreateMultipleDirectoryLevels()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testDirectoryPath1 = Path.Combine(currentPath, "testdirectory1");
      string testDirectoryPath2 = Path.Combine(testDirectoryPath1, "testdirectory2");

      FileUtils.EnsureDirectoryDeleted(testDirectoryPath2);
      FileUtils.EnsureDirectoryDeleted(testDirectoryPath1);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IDirectory directory = fileSystem.GetDirectory(testDirectoryPath2);

      Assert.AreEqual(false, directory.Exists);

      directory.Create();

      Assert.AreEqual(true, directory.Exists);

      IDirectory parent = directory.Parent;

      Assert.AreEqual(true, parent.Exists);
    }

    [Test]
    public void DeleteDirectory()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testDirectoryPath = Path.Combine(currentPath, "testdirectory");

      FileUtils.EnsureDirectoryExists(testDirectoryPath);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IDirectory directory = fileSystem.GetDirectory(testDirectoryPath);
      Assert.AreEqual(true, directory.Exists);

      directory.Delete();
      Assert.AreEqual(false, directory.Exists);
    }

    [Test]
    public void CopyDirectory()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testDirectoryPath = Path.Combine(currentPath, "testdirectory");
      string destinationDirectoryPath = Path.Combine(currentPath, "destinationdirectory");

      FileUtils.EnsureDirectoryDeleted(testDirectoryPath);
      FileUtils.EnsureDirectoryDeleted(destinationDirectoryPath);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IDirectory directory = fileSystem.GetDirectory(testDirectoryPath);
      directory.Create();

      FileUtils.EnsureFileExists(Path.Combine(testDirectoryPath, "test"));
      FileUtils.EnsureFileExists(Path.Combine(testDirectoryPath, "test.txt"));
      FileUtils.EnsureFileExists(Path.Combine(testDirectoryPath, "test1.txt"));
      FileUtils.EnsureDirectoryExists(Path.Combine(testDirectoryPath, "sub"));
      FileUtils.EnsureFileExists(Path.Combine(Path.Combine(testDirectoryPath, "sub"), "test1.txt"));

      IDirectory destinationDirectory = fileSystem.GetDirectory(destinationDirectoryPath);
      Assert.AreEqual(false, destinationDirectory.Exists);

      directory.CopyTo(destinationDirectoryPath);
      Assert.AreEqual(true, destinationDirectory.Exists);

      Assert.AreEqual(3, destinationDirectory.GetFiles().Count);
      Assert.AreEqual(true, destinationDirectory.GetFiles().Where(f => f.Name == "test").Any());
      Assert.AreEqual(true, destinationDirectory.GetFiles().Where(f => f.Name == "test.txt").Any());
      Assert.AreEqual(true, destinationDirectory.GetFiles().Where(f => f.Name == "test1.txt").Any());
      Assert.AreEqual(true, destinationDirectory.GetFiles().Where(f => f.Name == "test").Any());

      Assert.AreEqual(1, destinationDirectory.GetDirectories().Count);
      Assert.AreEqual(true, destinationDirectory.GetDirectories().Where(f => f.Name == "sub").Any());
    }
  }
}